package com;

import com.alibaba.druid.pool.DruidDataSource;
import com.javabean.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class JdbcTest {
    @Test
    public void t1(){
        //JDBCTemplate本质就是对jdbc封装的工具类
        //与我们之前自己定义MyJDBCUtil相同




        //JDBCTemplate没有像mybatis一样提供默认的连接池
        //所以需要我们自己创建连接池对象交由JDBCTemplate使用
        DruidDataSource druidDataSource=new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/day0920");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");

        //使用已经创建好的数据库连接池对象创建jdbcTemplate对象
        JdbcTemplate jdbcTemplate=new JdbcTemplate();
        jdbcTemplate.setDataSource(druidDataSource);
        //String sql="insert into user(username,password,phone) values(?,?,?)";
        //int bzdsb = jdbcTemplate.update(sql, "bzdsb", "123456", "13846899438");
        //System.out.println(bzdsb);
        String sql="select username,password from user where password=?";

        List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class),"dsb");
        for (User user : query) {
            System.out.println(user);
        }
    }
    @Test
    public void t2(){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-jdbc.xml");
        JdbcTemplate jdbc = (JdbcTemplate) applicationContext.getBean("jdbc");
        String sql="select username,password from user where password=?";
        List<User> query = jdbc.query(sql, new BeanPropertyRowMapper<>(User.class),"dsb");
        for (User user : query) {
            System.out.println(user);
        }
    }





















}
